<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="daterangeReleaseTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['report:report:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['report:report:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['report:report:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:report:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="60" align="center"/>
      <el-table-column  type="index" label="序号" width="100" align="center" :index="indexMethod" />
      <el-table-column label="标题" width="300"   prop="pstitle"/>
      <el-table-column label="上报人" width="200" align="center" prop="reportBy"/>
      <el-table-column label="单位" width="200" align="center" prop="reportUnit"/>
      <!-- 状态：1=草稿；2=已上报；3=已处理；4=已退回；5=已删除； -->
      <el-table-column label="办理情况" width="200" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">草稿</span>
          <span v-if="scope.row.status == 2">已上报</span>
          <span v-if="scope.row.status == 3">已处理</span>
          <span v-if="scope.row.status == 4">已退回</span>
          <span v-if="scope.row.status == 5">已删除</span>
        </template>

      </el-table-column>

      <el-table-column label="发布时间"  align="center" prop="releaseTime" width="280">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

<!--      <el-table-column label="上报时间" align="center" prop="reportTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->

      <!--      <el-table-column label="倾向性" align="center" prop="tendentiousness">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.ps_tendentiousness" :value="scope.row.tendentiousness"/>
              </template>
            </el-table-column>-->
      <!--      <el-table-column label="网址" align="center" prop="linkUrl" />-->
      <!--      <el-table-column label="媒体类型" align="center" prop="mediaType">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.ps_media" :value="scope.row.mediaType"/>
              </template>
            </el-table-column>-->
      <!--      <el-table-column label="来源网站" align="center" prop="srcWebset" />-->

      <!-- <el-table-column label="上报内容" align="center" prop="reportContent" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['report:report:edit']"-->
<!--          >修改-->
<!--          </el-button>-->

<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['report:report:remove']"
          >删除
          </el-button>-->

<!--          <el-button-->
<!--            type="text"-->
<!--            icon="el-icon-plus"-->
<!--            size="mini"-->
<!--            @click="report(scope.row)"-->
<!--          >上报-->
<!--          </el-button>-->
          <el-button
            v-hasRole="['admin','conservator']"
            size="mini"
            type="text"
            v-if="scope.row.status === 2"
            @click="handleDispose(scope.row)"
          >转处理
          </el-button>
<!--          <el-button-->
<!--            v-hasRole="['admin','conservator']"-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            v-if="scope.row.status === 3"-->
<!--            @click="report(scope.row)"-->
<!--          >已处理-->
<!--          </el-button>-->

          <el-button
            v-hasRole="['common']"
            size="mini"
            type="text"
            v-if="scope.row.status === 1"
            @click="handleAppear(scope.row)"
          >上报
          </el-button>
<!--          <el-button
            v-hasRole="['common']"
            size="mini"
            type="text"
            v-if="scope.row.status === 2"
            @click="report(scope.row)"
          >已上报
          </el-button>-->

          <el-button
            v-hasRole="['common']"
            size="mini"
            type="text"
            v-if="scope.row.status === 2"
            @click="handleWithdraw(scope.row)"
          >撤回
          </el-button>
<!--          <el-button-->
<!--            v-hasRole="['common']"-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            v-if="scope.row.status === 4"-->
<!--            @click="report(scope.row)"-->
<!--          >已撤回-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改舆情上报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="16">
            <el-form-item label="标题" prop="pstitle">
              <el-input v-model="form.pstitle" placeholder="请输入上报舆情标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="倾向性" prop="tendentiousness">
              <el-select style="width: 100%;" v-model="form.tendentiousness" placeholder="请选择倾向性">
                <el-option
                  v-for="dict in dict.type.ps_tendentiousness"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="16">
            <el-form-item label="网址" prop="linkUrl">
              <el-input v-model="form.linkUrl" placeholder="请输入网址"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="媒体类型" prop="mediaType">
              <el-select style="width: 100%;" v-model="form.mediaType" placeholder="请选择媒体类型">
                <el-option
                  v-for="dict in dict.type.ps_media"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="来源网站" prop="srcWebset">
              <el-input v-model="form.srcWebset" placeholder="请输入来源网站"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="发布时间" prop="releaseTime">
              <el-date-picker clearable
                              v-model="form.releaseTime"
                              type="date"
                              style="width: 100%;"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择发布时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-row :gutter="24" type="flex" justify="space-between">-->
<!--          <el-col :span="11">-->
<!--            <el-form-item label="上报人" prop="reportBy">-->
<!--              <el-input v-model="form.reportBy" placeholder="请输入上报人"/>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="11">-->
<!--            <el-form-item label="上报单位" prop="reportUnit">-->
<!--              <el-input v-model="form.reportUnit" placeholder="请输入上报单位"/>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

        <el-form-item label="上报内容">
          <editor v-model="form.reportContent" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="reported">上 报</el-button>-->
        <el-button type="primary" v-hasRole="['common']"  @click="submitForm(1)" >上 报</el-button>
        <el-button type="primary" @click="submitForm(0)">暂 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 处理 -->
    <el-dialog :title="title" :visible.sync="open1" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="16">
            <el-form-item label="标题" prop="pstitle">
              <el-input v-model="form.pstitle" placeholder="请输入上报舆情标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="倾向性" prop="tendentiousness">
              <el-select style="width: 100%;" v-model="form.tendentiousness" placeholder="请选择倾向性">
                <el-option
                  v-for="dict in dict.type.ps_tendentiousness"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="16">
            <el-form-item label="网址" prop="linkUrl">
              <el-input v-model="form.linkUrl" placeholder="请输入网址"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="媒体类型" prop="mediaType">
              <el-select style="width: 100%;" v-model="form.mediaType" placeholder="请选择媒体类型">
                <el-option
                  v-for="dict in dict.type.ps_media"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="来源网站" prop="srcWebset">
              <el-input v-model="form.srcWebset" placeholder="请输入来源网站"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="发布时间" prop="releaseTime">
              <el-date-picker clearable
                              v-model="form.releaseTime"
                              type="date"
                              style="width: 100%;"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择发布时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="上报人" prop="reportBy">
              <el-input v-model="form.reportBy" placeholder="请输入上报人"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="上报单位" prop="reportUnit">
              <el-input v-model="form.reportUnit" placeholder="请输入上报单位"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="上报内容">
          <editor v-model="form.reportContent" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="reported">上 报</el-button>-->
        <el-button type="primary"  @click="submitForm2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listReport, getReport, delReport, addReport, updateReport, dispose, appear, withdraw} from "@/api/report/report";

import {addPublicsentiment} from "@/api/publicsentiment/publicsentiment";


export default {
  name: "Report",
  dicts: ['ps_tendentiousness', 'ps_media'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 舆情上报表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 上报内容时间范围
      daterangeReleaseTime: [],
      // 上报内容时间范围
      daterangeReportTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        releaseTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pstitle: [
          {required: true, message: "上报舆情标题不能为空", trigger: "blur"}
        ],
        tendentiousness: [
          {required: true, message: "倾向性不能为空", trigger: "change"}
        ],
        releaseTime: [
          {required: true, message: "上报时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    indexMethod(index) {
       index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
       return index
     },
    // 详情
    handleDetails(row){
      this.$router.push({path:'/reportParticulars',query: {id:row.id}})
    },
    /** 查询舆情上报列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeReleaseTime && '' != this.daterangeReleaseTime) {
        this.queryParams.params["beginReleaseTime"] = this.daterangeReleaseTime[0];
        this.queryParams.params["endReleaseTime"] = this.daterangeReleaseTime[1];
      }
      if (null != this.daterangeReportTime && '' != this.daterangeReportTime) {
        this.queryParams.params["beginReportTime"] = this.daterangeReportTime[0];
        this.queryParams.params["endReportTime"] = this.daterangeReportTime[1];
      }
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pstitle: null,
        tendentiousness: null,
        linkUrl: null,
        mediaType: null,
        srcWebset: null,
        releaseTime: null,
        reportBy: null,
        reportUnit: null,
        reportTime: null,
        reportContent: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeReleaseTime = [];
      this.daterangeReportTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加舆情上报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改舆情上报";
      });
    },
    /** 转处理按钮操作*/
    handleDispose(row){
      this.reset();
      const id = row.id || this.ids
      const status = row.status
      getReport(id,status).then(response => {
        this.form = response.data;
        delete this.form.id
        this.$router.push({path:'/publicsentiment',query: {form:JSON.stringify(this.form)}})
      });
    },
    /** 上报操作按钮操作*/
    handleAppear(row){
      this.reset();
      const id = row.id || this.ids
      const status = row.status
      getReport(id,status).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "上报按钮操作";
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else if (status == 0)  {
            this.form.status = status
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              location. reload()
            });
          } else if (status == 1){
            this.form.status = status
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("上报成功");
              this.open = false;
              this.getList();
              location. reload()
            });
          }
        }
      });
    },
    /** 上报按钮 */
    submitForm2() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null && this.form.status == 2) {
            dispose(this.form).then(response => {
              this.open1 = false;
              this.getList();
            });
            addPublicsentiment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }else if (this.form.id != null && this.form.status == 1){
            appear(this.form).then(response => {
              this.$modal.msgSuccess("上报成功");
              this.open1 = false;
              this.getList();
              location. reload()
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除舆情上报编号为"' + ids + '"的数据项？').then(function () {
        return delReport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    /*撤回按钮操作*/
    handleWithdraw(row) {
      var data = {}
      data.id = row.id
      data.status = row.status
      this.$modal.confirm('是否确认撤回舆情上报编号为"' + row.id + '"的数据项？').then(function () {
        return withdraw(data);
      }).then(() => {
        this.$modal.msgSuccess("撤回成功");
        this.getList();
      }).catch(() => {
      });
      },


    /** 导出按钮操作 */
    handleExport() {
      this.download('report/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
